package com.sinosoft.emplmis.application.web;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.sinosoft.emplmis.jobless.entity.KnryTrackingrecordEntity;
import com.sinosoft.emplmis.jobless.service.KnryTrackingrecordService;

@Controller
@RequestMapping(value = "upload")
public class UploadController {

	@Autowired
	private KnryTrackingrecordService knryTrackingrecordService;

	@RequestMapping(value = "/uploadTrackingRecordImage", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void uploadTrackingRecordImage(String id,HttpServletResponse response,
			HttpServletRequest request) {
		KnryTrackingrecordEntity knryTrackingrecordEntity = new KnryTrackingrecordEntity();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		String ctxPath = request.getSession().getServletContext().getRealPath("/");
		ctxPath += File.separator + File.separator;
		System.out.println("ctxpath=" + ctxPath);
		// 创建文件夹

		File file = new File(ctxPath);
		if (!file.exists()) {
			file.mkdirs();
		}
		String fileName = null;
		@SuppressWarnings("unused")
		String responseStr = "";
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile mf = entity.getValue();
			fileName = mf.getOriginalFilename();// 获取原文件名
			File uploadFile = new File(ctxPath + fileName);
			try {
				byte[] sc = mf.getBytes();
				FileCopyUtils.copy(sc, uploadFile);
				knryTrackingrecordEntity.setId(id);
				knryTrackingrecordEntity.setPic(sc);
				knryTrackingrecordService.updateByPrimaryKey(knryTrackingrecordEntity);
				responseStr = "上传成功";
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**

	 * 处理上传图片显示

	 * 1根据应用id遍历应用的非矢量图片

	 * 2循环判断集合中的iconblob是否为空，如果不为空，将iconblob转化成输出流，重新获取文件路径

	 * 3return ResponsData

	 */
	@RequestMapping(value = "/selectFile", method = RequestMethod.GET)
	@ResponseBody
	public void selectFile(HttpServletResponse response, HttpServletRequest request,String id) throws IOException {
		KnryTrackingrecordEntity knryTrackingrecordEntity = knryTrackingrecordService.getpicById(id);
		File file = new File(request.getSession().getServletContext().getRealPath("/") + File.separator + "uploadFiles");
		response.setCharacterEncoding("utf-8");
		response.setContentType("doc");
		response.setHeader("Content-Disposition",
				"attachment; filename=" + new String(file.getName().getBytes("gb2312"), "utf-8"));
		OutputStream out = response.getOutputStream();
		byte[] img = knryTrackingrecordEntity.getPic();
		response.flushBuffer();
		if (img != null) {
			out.write(img);
			out.close();
		}
	}
}
